package icu.qx.pbs.controller;

import java.util.*;
import java.io.IOException;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import icu.qx.pbs.domain.*;
import icu.qx.pbs.domain.param.ProcedureParam;
import icu.qx.pbs.domain.param.RouteParam;
import icu.qx.pbs.domain.vo.ProcedureMaterial;
import icu.qx.pbs.service.IPbsProcedureRouteService;
import icu.qx.pbs.service.IPbsProuteProcedureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.pbs.service.IPbsProcedureMaterialService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 工序物料清单Controller
 *
 * @author yong
 * @date 2024-06-26
 */
@RestController
@RequestMapping("/pbs/procedureMaterial")
public class PbsProcedureMaterialController extends BaseController {
    @Autowired
    private IPbsProcedureMaterialService pbsProcedureMaterialService;
    @Autowired
    private IPbsProcedureRouteService pbsProcedureRouteService;
    @Autowired
    private IPbsProuteProcedureService pbsProuteProcedureService;

    /**
     * 查询工序物料清单列表
     */
    @GetMapping("/list")
    public AjaxResult list(PbsProcedureMaterial pbsProcedureMaterial) {
//        AjaxResult ajaxResult = success();
//        List<PbsProcedureMaterial> list = pbsProcedureMaterialService.selectPbsProcedureMaterialList(pbsProcedureMaterial);
//
//        if (list != null && !list.isEmpty()) {
//            Map<String, List<PbsProcedureMaterial>> collect = list.stream()
//                    .collect(Collectors.groupingBy(
//                            PbsProcedureMaterial::getProcudureCode,
//                            LinkedHashMap::new, Collectors.toList()
//                    ));
//
//            PbsProcedureRoute pbsProcedureRoute = pbsProcedureRouteService.getPbsProcedureRouteNameByProuteCode(pbsProcedureMaterial.getPmProuteCode());
//
//            List<PbsProcedure> pbsProcedures = new ArrayList<>();
//            Map<String, List<GbsGood>> gbsGoods = new HashMap<>();
//            collect.forEach((k, v) -> {
//                if (v == null || v.isEmpty()) {
//                    return;
//                }
//                PbsProcedure pbsProcedure = new PbsProcedure();
//                pbsProcedure.setProcedureCode(k);
//                pbsProcedure.setProcedureName(v.get(0).getPbsProcedure().getProcedureName());
//                pbsProcedures.add(pbsProcedure);
//
//                List<GbsGood> gbsGoodList = new ArrayList<>();
//                v.forEach(e -> {
//                    GbsGood gbsGood = e.getGbsGood();
//                    gbsGoodList.add(gbsGood);
//                });
//                gbsGoods.put(k, gbsGoodList);
//            });
//
//            ajaxResult.put("data", pbsProcedures);
//            ajaxResult.put("route", pbsProcedureRoute);
//            ajaxResult.put("procedureMaterials", gbsGoods);
//        }
//        return ajaxResult;

        // 获取工艺路线
        PbsProcedureRoute pbsProcedureRoute = pbsProcedureRouteService.selectPbsProcedureRouteByProuteCode(pbsProcedureMaterial.getPmProuteCode());
        RouteParam routeParam = new RouteParam();
        routeParam.setProuteCode(pbsProcedureRoute.getProuteCode());
        routeParam.setProuteName(pbsProcedureRoute.getProuteName());

        // 获取工序
        PbsProuteProcedure pbsProuteProcedure = new PbsProuteProcedure();
        pbsProuteProcedure.setPpProuteCode(pbsProcedureRoute.getProuteCode());
        List<PbsProuteProcedure> pbsProuteProcedures = pbsProuteProcedureService.selectPbsProuteProcedureList(pbsProuteProcedure);
        List<ProcedureParam> procedureParams = pbsProuteProcedures.stream().map(e -> {
            ProcedureParam procedureParam = new ProcedureParam();
            procedureParam.setProcedureName(e.getPbsProcedure().getProcedureName());
            procedureParam.setProcedureCode(e.getPbsProcedure().getProcedureCode());
            return procedureParam;
        }).collect(Collectors.toList());


        // 获取物料
        procedureParams.forEach(e -> {
            PbsProcedureMaterial procedureMaterial = new PbsProcedureMaterial();
            procedureMaterial.setProcudureCode(e.getProcedureCode());
            procedureMaterial.setPmProuteCode(pbsProcedureRoute.getProuteCode());
            procedureMaterial.setProcedureMBom(pbsProcedureMaterial.getProcedureMBom());
            procedureMaterial.setProcedureMBV(pbsProcedureMaterial.getProcedureMBV());
            List<PbsProcedureMaterial> list = pbsProcedureMaterialService.selectPbsProcedureMaterialList(procedureMaterial);
            list.forEach(System.err::println);
            System.err.println();
            e.setPbsProcedureMaterialList(list);
        });

        routeParam.setProcedureParams(procedureParams);
        return success(routeParam);
    }

    @GetMapping("/routeCode/{routeCode}")
    public R<List<PbsProcedureMaterial>> listByRouteCode(@PathVariable("routeCode") String routeCode) {
        PbsProcedureMaterial pbsProcedureMaterial = new PbsProcedureMaterial();
        pbsProcedureMaterial.setPmProuteCode(routeCode);
        List<PbsProcedureMaterial> list = pbsProcedureMaterialService.selectPbsProcedureMaterialList(pbsProcedureMaterial);
        return R.ok(list);
    }

    @GetMapping("/getSelfDetail")
    public AjaxResult getSelfDetail(PbsProcedureMaterial pbsProcedureMaterial) {
        return success(pbsProcedureMaterialService.selectSelfData(pbsProcedureMaterial));
    }

    /**
     * 新增工序物料清单
     */
    @Log(title = "工序物料清单", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Integer> add(@RequestBody List<PbsProcedureMaterial> pbsProcedureMaterials) {
        return R.ok(pbsProcedureMaterialService.insertPbsProcedureMaterials(pbsProcedureMaterials));
    }

    /**
     * 删除工序物料清单
     */
    @DeleteMapping
    public AjaxResult remove(@RequestBody PbsProcedureMaterial pbsProcedureMaterial) {
        return success(pbsProcedureMaterialService.deletePbsProcedureMaterialByPris(pbsProcedureMaterial));
    }
}
